/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.adv;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.aliyuncs.AliyuncsUtil;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.ImgUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkStaticAdv;
import com.thinkgem.jeesite.modules.hk.sys.dao.adv.HkStaticAdvDao;
import org.springframework.web.util.UriUtils;

/**
 * 静态广告Service
 * @author 罗天文
 * @version 2018-05-10
 */
@Service
@Transactional(readOnly = true)
public class HkStaticAdvService extends CrudService<HkStaticAdvDao, HkStaticAdv> {

	public HkStaticAdv get(String id) {
		return super.get(id);
	}
	
	public List<HkStaticAdv> findList(HkStaticAdv hkStaticAdv) {
		return super.findList(hkStaticAdv);
	}
	
	public Page<HkStaticAdv> findPage(Page<HkStaticAdv> page, HkStaticAdv hkStaticAdv) {
		return super.findPage(page, hkStaticAdv);
	}
	
	@Transactional(readOnly = false)
	public void save(HkStaticAdv hkStaticAdv) {

		String coverImgSrc=hkStaticAdv.getCoverImgSrc();
		if (!coverImgSrc.contains(Global.getFileServer())) {
			coverImgSrc=(Global.getFileServer().trim() + hkStaticAdv.getCoverImgSrc());
			Map<String, String> map = ImgUtils.getImgWidthHeight(hkStaticAdv.getCoverImgSrc());
			hkStaticAdv.setCoverImgWidth(map.get("width"));
			hkStaticAdv.setCoverImgHeight(map.get("height"));
		}
		if(StringUtils.isNotEmpty(hkStaticAdv.getAimgSrc())) {

			String medisPath = Global.getUserfilesBaseDir()+ hkStaticAdv.getAimgSrc();
			try {
				medisPath = UriUtils.decode(medisPath, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new ServiceException("上传失败");
			}
			StringBuffer sb=new StringBuffer();

			String videoId = AliyuncsUtil.uploadVideo(hkStaticAdv.getTitle(), medisPath, coverImgSrc, sb.toString() );
			if(StringUtils.isEmpty(videoId)){
				throw new ServiceException("上传失败");
			}
			hkStaticAdv.setImgSrc(videoId);
		}
		super.save(hkStaticAdv);
	}
	
	@Transactional(readOnly = false)
	public void delete(HkStaticAdv hkStaticAdv) {
		super.delete(hkStaticAdv);
	}
	
}